﻿

using Awsc.DataAcquisitionSys.Core.Common;

namespace Awsc.DataAcquisitionSys.Core.Model.Config
{
    public class DeviceConfig
    {

        public event EventHandler<SensorConfig> SensorConfigChanged;
        public event EventHandler<SensorConfig> SensorConfigAdded;


        /// <summary>
        /// 系统名称
        /// </summary>
        public string SysName { get; set; }
        /// <summary>
        /// 设备类型
        /// </summary>
        public DeviceTypeEnum DeviceType { get; set; }
        /// <summary>
        /// 监控端串口号
        /// </summary>
        public string LocalComPort { get; set; }
        /// <summary>
        /// 监控端波特率
        /// </summary>
        public int LocalRate { get; set; }
        /// <summary>
        /// 最大无支撑距离
        /// </summary>
        public int MaxUnsurportLength { get; set; }
        /// <summary>
        /// 是否开启远传模式
        /// </summary>
        public bool RemoteMode { get; set; }
        /// <summary>
        /// 距离传感器偏离
        /// </summary>
        public int DisSensorDis { get; set; }
        /// <summary>
        /// 是否开启报警
        /// </summary>
        public bool BeUnsurportLengthAlarmEnabled { get; set; }
        /// <summary>
        /// 摄像头配置
        /// </summary>
        public List<CameraConfig> Cameras { get; set; } = new List<CameraConfig>();
        /// <summary>
        /// 采集器配置
        /// </summary>
        public List<CollectorConfig> Collectors { get; set; } = new List<CollectorConfig>();
        public List<SensorConfig> GetAllSensorConfig()
        {
            List<SensorConfig> allSensors = new List<SensorConfig>();
            foreach (var collector in Collectors)
            {
                allSensors.AddRange(collector.InputRegisters);
                allSensors.AddRange(collector.OutputRegisters);
                allSensors.AddRange(collector.InputCoils);
                allSensors.AddRange(collector.OutputCoils);
            }
            return allSensors;
        }

        public SensorConfig GetSensorConfig(string sensorName)
        {
            ArgumentAssert.NotNullOrEmpty(sensorName);
            var allSensorConfigs = GetAllSensorConfig();
            return allSensorConfigs.FirstOrDefault(s => s.Name == sensorName);
        }


        public IEnumerable<RegisterConfig> GetAllRegisterConfigs()
        {
            var allSensorConfigs = GetAllSensorConfig();
            return allSensorConfigs.Where(s => s is RegisterConfig).Cast<RegisterConfig>();
        }
        public IEnumerable<CoilConfig> GetAllCoilConfigs()
        {
            var allSensorConfigs = GetAllSensorConfig();
            return allSensorConfigs.Where(s => s is CoilConfig).Cast<CoilConfig>();
        }

        public RegisterConfig GetRegisterConfig(string sensorName)
        {
            ArgumentAssert.NotNullOrEmpty(sensorName);
            var allSensorConfigs = GetAllSensorConfig();
            return allSensorConfigs.FirstOrDefault(s => s.Name == sensorName) as RegisterConfig;
        }

        public CoilConfig GetCoilConfig(string sensorName)
        {
            ArgumentAssert.NotNullOrEmpty(sensorName);
            var allSensorConfigs = GetAllSensorConfig();
            return allSensorConfigs.FirstOrDefault(s => s.Name == sensorName) as CoilConfig;
        }


        public void AddSensorConfig(SensorConfig sensorConfig)
        {
            ArgumentAssert.NotNull(sensorConfig);
            ArgumentAssert.NotNullOrEmpty(sensorConfig.CollectorName);
            ArgumentAssert.NotNullOrEmpty(sensorConfig.Name);

            if (sensorConfig == null) return;
            var collector = Collectors.FirstOrDefault(c => c.Name == sensorConfig.CollectorName);
            if (collector == null)
            {
                throw new Exception($"不存在名为\"{sensorConfig.CollectorName}\"的采集器。");
            }

            var oldSensorConfig = collector.AllSensorConfigs.FirstOrDefault(s => s.Name == sensorConfig.Name);
            if(oldSensorConfig!=null)
            {
                throw new Exception($"采集器{sensorConfig.CollectorName}上已存在名为\"{sensorConfig.Name}\"的传感器。");
            }

            if (sensorConfig is RegisterConfig register)
            {

                collector.InputRegisters.Add(register);
            }
            else if (sensorConfig is CoilConfig coil)
            {
                collector.InputCoils.Add(coil);
            }
            SensorConfigAdded?.Invoke(this, sensorConfig);
        }

        public void UpdateSensorConfig(SensorConfig sensorConfig)
        {
            ArgumentAssert.NotNull(sensorConfig);
            ArgumentAssert.NotNullOrEmpty(sensorConfig.CollectorName);
            ArgumentAssert.NotNullOrEmpty(sensorConfig.Name);
            if (sensorConfig == null) return;
            var collector = Collectors.FirstOrDefault(c => c.Name == sensorConfig.CollectorName);
            if (collector == null)
            {
                throw new Exception($"不存在名为\"{sensorConfig.CollectorName}\"的采集器。");
            }

            var oldSensorConfig = collector.AllSensorConfigs.FirstOrDefault(s => s.Name == sensorConfig.Name);
            if (oldSensorConfig == null)
            {
                throw new Exception($"采集器{sensorConfig.CollectorName}上不存在名为\"{sensorConfig.Name}\"的传感器。");
            }

            if (sensorConfig is RegisterConfig register)
            {

                collector.InputRegisters.Remove(oldSensorConfig as RegisterConfig);
                collector.InputRegisters.Add(register);
            }
            else if (sensorConfig is CoilConfig coil)
            {
                collector.InputCoils.Remove(oldSensorConfig as CoilConfig);
                collector.InputCoils.Add(coil);
            }
            SensorConfigAdded?.Invoke(this, sensorConfig);
        }

    }
}
